#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "stm32f10x.h"

#include "public_lib.h"
#include "serial.h"
#include "system_timer.h"
#include "beep_task.h"
#include "gpio.h"

#define BEEP_TASK_PERIOD     (50)
#define BEEP_MIN_PERIOD      (10)

typedef struct
{
    s_gpio_cfg beep_hw_cfg;
    unsigned char enable;
    unsigned char period;  //ms
    unsigned char status;
        unsigned char index;
      unsigned char loud;  //us
}s_beep_task_info;

static s_beep_task_info info = 
{
    {RCC_APB2Periph_GPIOB,GPIOB,GPIO_Pin_5},
    0, 10,0,0,0
};


void start_beep_task(void *args)
{
    GPIO_InitTypeDef GPIO_InitStructure;
   
    RCC_APB2PeriphClockCmd(info.beep_hw_cfg.io_rcc,ENABLE);
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    
    GPIO_InitStructure.GPIO_Pin   = info.beep_hw_cfg.pin;
    GPIO_Init(info.beep_hw_cfg.port, &GPIO_InitStructure);
    GPIO_SetBits(info.beep_hw_cfg.port, info.beep_hw_cfg.pin);
        info.index = info.period;
}

int beep_task_run(void *args)
{
    if(info.enable)
    {    
        info.index --;
        if(info.index <= 0)
        {
            if(info.status==0)
            {
                GPIO_SetBits(info.beep_hw_cfg.port, info.beep_hw_cfg.pin);
            }
            else
            {
                GPIO_ResetBits(info.beep_hw_cfg.port, info.beep_hw_cfg.pin);
            }
            info.status ^= 0x01;
                        info.index = info.period;
        }    
    }
        else if(info.loud)
        {
              GPIO_SetBits(info.beep_hw_cfg.port, info.beep_hw_cfg.pin);            
              delay_us(info.loud);
        GPIO_ResetBits(info.beep_hw_cfg.port, info.beep_hw_cfg.pin);
        }
    
    return 0;
}

int beep_task_manage(int argc, char **argv, void *args)
{
    int err_code = -1;

    if(argc >= 2)
    {
       
        if(strcasecmp(argv[0], "beep")==0)
        {
            if(strcasecmp(argv[1], "enable") == 0)
            {
                info.enable = 1;
                info.status = 0;
                GPIO_SetBits(info.beep_hw_cfg.port, info.beep_hw_cfg.pin);
            }
            else if(strcasecmp(argv[1], "disable") == 0)
            {                
                info.enable = 0;
                GPIO_SetBits(info.beep_hw_cfg.port, info.beep_hw_cfg.pin);
            }
            else if(strcasecmp(argv[1], "period") == 0)
            {                
                if(argc == 3)
                {
                    info.period = (unsigned char)atoi(argv[2]);
                                        if(info.period==0){info.period=1;}
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s period[1~256ms]", argv[0], argv[1], argv[0], argv[1]);
                }
            }    
                        else if(strcasecmp(argv[1], "loud") == 0)                        
                        {
                              if(argc == 3)
                {    
                                        if(0==info.enable)
                                        {
                                              info.loud = (unsigned char)atoi(argv[2]);                                                
                                        }
                                        else
                                        {
                                              serial_writeline("please stop tick-tick");
                                        }                   
                }
                else
                {
                                    serial_writeline("%s %s usage:  %s %s strength[0-stop, 1~255:strength]", argv[0], argv[1], argv[0], argv[1]);
                }
                        }
        }        
        else{/* not for me, do nothing */}
    }
    else{/* not for me, do nothing */}

    return err_code;
}
